package web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;
import sevice.ServiceDAOFactory;
import sevice.UserService;
import util.EmailCode;
import util.Md5Code;

@WebServlet("/regist.do")
public class RegistServlet extends HttpServlet {
	public static final String one = "0";
	public static final String two = "1";
	public static final String three = "2";
	private static final long serialVersionUID = 5774398456560595952L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method != null) {
			switch (method) {
			case RegistServlet.one:
				registTo(req, resp);
				break;
			case RegistServlet.two:
				registStep1(req, resp);
				break;
			case RegistServlet.three:
				registStep2(req, resp);
				break;
			}
		}
	}

	private void registStep2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String code = req.getParameter("code");
		String uid = EmailCode.getUid(code);
		if (uid == null) {
			req.setAttribute("verify_err", "错误");
			req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
			return;
		}
		String email = uid;
		String vCode = EmailCode.getUUID(code);
		UserService userService = ServiceDAOFactory.getUserDAO();
		try {
			User u = userService.findByEmail(email);
			if (u != null && vCode.trim().equals(u.getEmailVerifyCode())) {
				u.setEmailVerify(true);
				userService.update(u);
				req.getRequestDispatcher("/WEB-INF/view/user/register_ok.jsp").forward(req, resp);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void registStep1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		User user = new User();
		try {
			UserService user1 = ServiceDAOFactory.getUserDAO();
			String sq = req.getParameter("email");
			user.setEmail(sq);
			user.setNickname(req.getParameter("nickname"));
			String pwd = Md5Code.createMd5Code(req.getParameter("password"));
			user.setPassword(pwd);
			user.setUserIntegral(10);
			user.setEmailVerify(false);

			String email = "S" + (int) ((Math.random() * 10) * 100000);
			String emailCodes = email;
			SendMail ss = new SendMail();
			ss.sendMail(user.getEmail(), "��֤��" + emailCodes + "-" + req.getParameter("email"), "����լ��֤");
			System.out.println("1");

			user.setEmailVerifyCode(emailCodes);
			user.setLastLoginTime(System.currentTimeMillis());
			user.setLastLoginIp(req.getRemoteAddr());
			user1.save(user);
			req.getSession().setAttribute("s_user", user);

		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getRequestDispatcher("/WEB-INF/view/user/verify_form.jsp").forward(req, resp);
	}

	private void registTo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

}
